package org.practice.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.practice.cart.Cart;
import org.practice.form.CartItemForm;
import org.practice.model.User;
import org.practice.util.Constant;
import org.practice.util.CookieUtil;

public class CartAction extends CartMappingDispatchAction {

	public ActionForward show(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		CartServiceImpl cart = this.getCart(request);
		if (cart.isEmpty()) {
			ActionMessages errors = new ActionMessages();
			errors.add("error.cart.empty",
					new ActionMessage("error.cart.empty"));
			saveErrors(request, errors);

		}
		return mapping.findForward("success");
	}

	public ActionForward add(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		CartServiceImpl cart = getCart(request);
		CartItemForm f = (CartItemForm) form;
		cart.addCartItem(f.getProductId());
		saveToCookie(response, cart);
		return mapping.findForward("success");
	}

	public ActionForward update(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		CartServiceImpl cart = getCart(request);
		CartItemForm f = (CartItemForm) form;
		cart.updateNum(f.getProductId(), f.getNum());
		saveToCookie(response, cart);
		return mapping.findForward("success");
	}

	public ActionForward drop(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		CartServiceImpl cart = getCart(request);
		CartItemForm f = (CartItemForm) form;
		cart.drop(f.getProductId());
		saveToCookie(response, cart);
		return mapping.findForward("success");
	}

	public ActionForward recovery(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		CartServiceImpl cart = getCart(request);
		CartItemForm f = (CartItemForm) form;
		cart.recovery(f.getProductId());
		saveToCookie(response, cart);
		return mapping.findForward("success");
	}

	private CartServiceImpl getCart(HttpServletRequest request) throws Exception {
		HttpSession session = request.getSession();
		User user = (User) session.getAttribute(Constant.SESSION_KEY_USER);
		CartServiceImpl cart = (CartServiceImpl) session.getAttribute(Constant.SESSION_KEY_CART);
		if (cart == null) {
			cart = new CartServiceImpl(user.getId());
			String cartStr = CookieUtil.findCookie(request,
					Constant.COOKIE_KEY_CARTID);
			if (cartStr != null) {
				cart.load(cartStr);
			}
			session.setAttribute(Constant.SESSION_KEY_CART, cart);
		}
		return cart;
	}

	private void saveToCookie(HttpServletResponse response, CartServiceImpl cart)
			throws Exception {
		CookieUtil.addCookie(response, Constant.COOKIE_KEY_CARTID, cart.serialize());
	}
}
